package com.android.equipment.base

import android.app.Activity
import android.content.pm.ActivityInfo
import android.os.Bundle
import android.view.View
import android.view.Window
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.viewbinding.ViewBinding
import cn.bingoogolapple.swipebacklayout.BGASwipeBackHelper
import com.android.equipment.R
import com.android.equipment.utils.ActivityManager
import com.android.equipment.utils.StatusBarUtil
import com.gyf.immersionbar.ImmersionBar


abstract class BaseActivity<VB: ViewBinding> : AppCompatActivity(), BGASwipeBackHelper.Delegate {
    lateinit var binding: VB
    var mContext: Activity? = null
    lateinit  var mSwipeBackHelper: BGASwipeBackHelper


    public override fun onCreate(savedInstanceState: Bundle?) {
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        window.setSoftInputMode(
            WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN or
                    WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN
        )
        initSwipeBackFinish()
        super.onCreate(savedInstanceState)
        StatusBarUtil.setTranslucent(this) //设置状态栏颜色 全透明
        //设置竖屏
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED

        // 设置屏幕常亮
        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)





        mContext = this
        //将Activity实例添加到AppManager的堆栈
        ActivityManager.getInstance().add(this)
        binding = setLayoutContent()
        setContentView(binding.root)
        setStatusBar()

//        val controller = WindowCompat.getInsetsController(window, binding.root)
//        controller.hide(WindowInsetsCompat.Type.systemBars())

        try {
            initView(savedInstanceState)
        } catch (e: Exception) {
            e.printStackTrace()
        }
        initEvent()
        initData()
    }

    protected abstract fun setLayoutContent(): VB



    open fun setStatusBar() {
        ImmersionBar.with(this)
            .fullScreen(true)
            .statusBarDarkFont(true)
            .init()
    }


    /**
     * 初始化View
     */

    protected abstract fun initView(savedInstanceState: Bundle?)


    /**
     * 事件监听
     */
    protected open fun initEvent() {}

    /**
     * 数据加载
     */
    protected abstract fun initData()

    private fun initSwipeBackFinish() {
        mSwipeBackHelper = BGASwipeBackHelper(this, this)

        // 「必须在 Application 的 onCreate 方法中执行 BGASwipeBackHelper.init 来初始化滑动返回」
        // 下面几项可以不配置，这里只是为了讲述接口用法。

        // 设置滑动返回是否可用。默认值为 true
        mSwipeBackHelper.setSwipeBackEnable(true)
        // 设置是否仅仅跟踪左侧边缘的滑动返回。默认值为 true
        mSwipeBackHelper.setIsOnlyTrackingLeftEdge(true)
        // 设置是否是微信滑动返回样式。默认值为 true
        mSwipeBackHelper.setIsWeChatStyle(true)
        // 设置阴影资源 id。默认值为 R.drawable.bga_sbl_shadow
        mSwipeBackHelper.setShadowResId(R.drawable.bga_sbl_shadow)
        // 设置是否显示滑动返回的阴影效果。默认值为 true
        mSwipeBackHelper.setIsNeedShowShadow(true)
        // 设置阴影区域的透明度是否根据滑动的距离渐变。默认值为 true
        mSwipeBackHelper.setIsShadowAlphaGradient(true)
        // 设置触发释放后自动滑动返回的阈值，默认值为 0.3f
        mSwipeBackHelper.setSwipeBackThreshold(0.3f)
        // 设置底部导航条是否悬浮在内容上，默认值为 false
        mSwipeBackHelper.setIsNavigationBarOverlap(false)
    }


    /**
     * 是否支持滑动返回。这里在父类中默认返回 true 来支持滑动返回，如果某个界面不想支持滑动返回则重写该方法返回 false 即可
     *
     * @return
     */
    override fun isSupportSwipeBack(): Boolean {
        return false
    }

    /**
     * 正在滑动返回
     *
     * @param slideOffset 从 0 到 1
     */
    override fun onSwipeBackLayoutSlide(slideOffset: Float) {
    }

    /**
     * 没达到滑动返回的阈值，取消滑动返回动作，回到默认状态
     */
    override fun onSwipeBackLayoutCancel() {
    }

    /**
     * 滑动返回执行完毕，销毁当前 Activity
     */
    override fun onSwipeBackLayoutExecuted() {
        mSwipeBackHelper.swipeBackward()
    }

    override fun onBackPressed() {
        // 正在滑动返回的时候取消返回按钮事件
        if (mSwipeBackHelper.isSliding) {
            return
        }
        mSwipeBackHelper.backward()
    }




    override fun onResume() {
        super.onResume()
        mContext = this
    }


    override fun onRestart() {
        super.onRestart()
    }


    override fun onPause() {
        super.onPause()
    }


    override fun onStop() {
        super.onStop()
    }


    override fun onDestroy() {
        super.onDestroy()
        //将Activity实例从AppManager的堆栈中移除
        ActivityManager.getInstance().removeActivity(this)
        window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

//    override fun onWindowFocusChanged(hasFocus: Boolean) {
//        super.onWindowFocusChanged(hasFocus)
//        if (hasFocus) {
//            hideNavigationBar()
//        }
//    }
//


}
